package com.yuyu.common.result;

import java.io.Serializable;

/**
 * @author yuyu
 * @date 2018/12/27
 */
public class Result<T> implements Serializable{

    private Integer code;

    private String msg;

    private T data;

    public Result() {
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(T data) {
        this.data = data;
        this.setCode(ResultCode.SUCCESS.getCode());
        this.setMsg(ResultCode.SUCCESS.getMsg());
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
            "code=" + code +
            ", msg='" + msg + '\'' +
            ", data=" + data +
            '}';
    }

    public static Result success(){
        return new Result(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg());
    }
    public static Result error(){
        return new Result(ResultCode.ERROR.getCode(),ResultCode.ERROR.getMsg());
    }
    public static Result error(String msg){
        return new Result(ResultCode.ERROR.getCode(),msg);
    }
    public static Result notLogin(){
        return new Result(ResultCode.NOT_LOGIN.getCode(),ResultCode.NOT_LOGIN.getMsg());
    }
    public static Result badRequest(){
        return new Result(ResultCode.BAD_REQUEST.getCode(),ResultCode.BAD_REQUEST.getMsg());
    }
}
